#! /usr/bin/env python
# -*- coding: utf-8 -*-

import requests
import lxml.html

import sys


def loop_for_parse(t):
    result = []
    support_scheme = ["http",
                      "socks4",
                      "socks5"]
    try:
        assert t in support_scheme
    except:
        raise NotImplementedError
    for i in range(10):
        url = "http://cnproxy.com/proxy" + str(i + 1) + ".html"
        try:
            print "[*] Parsing %s now" % url
            proxy = parse_proxy_list(url, t)
        except Exception, e:
            print e
            break
        result += proxy
    return result


def parse_proxy_list(url, t):
    rep_dic = {"v": "3",
               "m": "4",
               "a": "2",
               "l": "9",
               "q": "0",
               "b": "5",
               "i": "7",
               "w": "6",
               "r": "8",
               "c": "1"}
    r = requests.get(url)
    assert r.ok
    #----------------------------------------
    proxy_list = []
    root = lxml.html.fromstring(r.content)
    ips = root.xpath('//tr/td[1]/text()')[1::]
    ports = root.xpath('//tr/td[1]/script/text()')[1::]
    tps = root.xpath('//tr/td[2]/text()')[1::]
    for ip, port, tp in zip(ips, ports, tps):
        if tp.lower() != t:
            # print "Type Wrong %s" % tp
            continue
        port = port.replace("+", "")
        port = port.replace('document.write(":"', '')
        port = port.replace(')', '')
        for alpha, digit in rep_dic.iteritems():
            port = port.replace(alpha, digit)
        # print ip, port
        proxy_list.append(ip + ':' + port)
    return proxy_list

if __name__ == '__main__':
    t = sys.argv[1]
    proxy_list = loop_for_parse(t)
    for i in range(len(proxy_list)):
        # print proxy_list[i]
        proxy_list[i] += '\n'
    with open('proxy_list/' + t + '_proxy_cnproxy.txt', 'wb') as f:
        f.writelines(proxy_list)
